﻿using ASmile.Helpers;
using System;
using System.Collections.Generic;
using System.Text;

namespace ASmile
{
    /// <summary>
    /// 用于查询分页所使用的参数基类
    /// </summary>
    public class ArgPageData
    {
        /// <summary>
        /// 页码
        /// </summary>
        public int PageNo { set; get; }
        /// <summary>
        /// 每页大小
        /// </summary>
        public int PageSize { set; get; }
        /// <summary>
        /// 搜索值
        /// </summary>
        public string SearchVal { set; get; }
        /// <summary>
        /// 返回搜索值的模糊查询内容(返回 %{SearchVal}%)
        /// 如果搜索值包含 % 则不直接返回 SearchVal
        /// 如果搜索开始包含 = 则不用 %% 模糊
        /// </summary>
        public string SearchValLike
        {
            get
            {
                if (SearchVal?.StartsWith("=") == true && SearchVal.Length > 1)
                {
                    return SearchVal.Substring(1);
                }
                if (SearchVal?.Contains("%") == true) return SearchVal;
                return $"%{SearchVal}%";
            }
        }
        /// <summary>
        /// 其它的查询内容
        /// </summary>
        public object TagData { set; get; }
        /// <summary>
        /// 查询时是否包含无效数据的共通参数
        /// </summary>
        public bool HasInvalid { set; get; }
        /// <summary>
        /// 是否撤销查询，撤销后不再进行查询，直接返回空数据
        /// </summary>
        public bool IsCancelQuery { set; get; }

        public DictExtend ExpandData { set; get; } = new DictExtend();

        ///// <summary>
        ///// 扩展字典参数
        ///// </summary>
        //public Dictionary<string, string> ExpandDict { get; set; }
        /// <summary>
        /// 清除扩展参数的所有数据
        /// </summary>
        public void ClearExpandDict()
        {
            ExpandData?.Clear();
        }
        /// <summary>
        /// 获取扩展参数对象
        /// </summary>
        public TRet GetExpandDict<TRet>() where TRet : class, new()
        {
            if (ExpandData == null) return null;
            return ExpandData.GetObject<TRet>();
        }
        /// <summary>
        /// 设置对象到 扩展参数 ExpandData 赋值，允许多次执行，属性名相同覆盖
        /// </summary>
        public void SetExpandDict(object obj)
        {
            if (ExpandData == null)
            {
                ExpandData = new DictExtend();
            }
            ExpandData.SetObject(obj);
        }
        /// <summary>
        /// 设置扩展字典的字符串类型
        /// </summary>
        public void SetExpandDict(string key, string val)
        {
            if (ExpandData == null)
            {
                ExpandData = new DictExtend();
            }
            ExpandData[key] = val;
        }
        /// <summary>
        /// 设置扩展字典的日期类型
        /// </summary>
        public void SetExpandDict(string key, DateTime val)
        {
            SetExpandDict(key, $"{val:yyyy-MM-dd HH:mm:ss}");
        }
        /// <summary>
        /// 设置扩展字典的其它值类型
        /// </summary>
        public void SetExpandDict(string key, object val)
        {
            SetExpandDict(key, $"{val}");
        }
        /// <summary>
        /// 获取扩展字典值
        /// </summary>
        public string GetExpandValue(string key)
        {
            if (ExpandData == null) return null;
            return ExpandData.GetValue(key);
        }
        /// <summary>
        /// 获取扩展参数字典强类型值
        /// </summary>
        public T GetExpandValue<T>(string key)
        {
            if (ExpandData == null) return default(T);
            return ExpandData.GetValue<T>(key);
        }
        /// <summary>
        /// 高级搜索明细集合
        /// </summary>
        public List<AdSearchDetail> AdSearchList { set; get; } = new List<AdSearchDetail>();

        public void ClearADList()
        {
            AdSearchList?.Clear();
        }
    }

    /// <summary>
    /// 高级搜索明细
    /// </summary>
    public class AdSearchDetail
    {
        /// <summary>
        /// 连接符
        /// </summary>
        public string ConnectorSignStr { set; get; }
        /// <summary>
        /// 比较符
        /// </summary>
        public string CompareSignStr { set; get; }
        /// <summary>
        /// 字段名
        /// </summary>
        public string FieldName { set; get; }
        /// <summary>
        /// 搜索字符串
        /// </summary>
        public string SearchValue { set; get; }
        /// <summary>
        /// 数据类型字符串
        /// </summary>
        public string DataTypeStr { set; get; }
    }
}
